package com.handlebars;

import com.domain.Student;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
import com.github.jknack.handlebars.io.TemplateLoader;

import java.util.Arrays;

/**
 * Created by pengfei on 16/6/13.
 */
public class Test {
    public static void main(String[] args) throws Exception{
        loadFromHandlebars();
    }

    /**
     *  可以直接使用字符串来渲染
     */
    public static void test() throws Exception{
        Handlebars handlebars = new Handlebars();
        Template template = handlebars.compileInline("Hello {{this}}!");
        System.out.println(template.apply("xxxxx"));
    }

    public static void loadFromHandlebars() throws Exception{
        TemplateLoader loader = new ClassPathTemplateLoader();
        loader.setPrefix("/templates");
        loader.setSuffix(".hbs");
        Handlebars handlebars = new Handlebars(loader);

        Template template = handlebars.compile("mytemplate");
        Student s1 = new Student();
        s1.setName("张三");
        s1.setAge(11);
        s1.setFavorite(Arrays.asList("足球","篮球","钢球"));

        Student s2= new Student();
        s2.setName("李四");
        s2.setAge(22);
        s2.setFavorite(Arrays.asList("架子鼓","吉他","小提琴"));

        System.out.println(template.apply(Arrays.asList(s1,s2)));
    }
    public static void xx(){

    }
}
